package com.fasteat.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasteat.common.entity.Result;
import com.fasteat.mall.entity.Address;
import com.fasteat.mall.entity.Orderform;
import com.fasteat.mall.service.AddressService;
import com.fasteat.mall.service.OrderformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hy
 * @since 2021-01-19
 */
@RestController
@CrossOrigin
@Api(description = "收货地址接口")
@RequestMapping("/mall/address")
public class AddressController {
	@Autowired
	private AddressService addressService;

	/**
	 * 新增收货地址
	 *
	 * @param address
	 * @return
	 */
	@ApiOperation(value = "新增收货地址")
	@PostMapping
	public Result save(@RequestBody Address address) {
		if (address == null) {
			return Result.error("请勿添加空地址");
		}
		boolean result = addressService.save(address);
		return result ?
				Result.success("添加成功")
				:
				Result.error("添加失败");
	}

	/**
	 * 根据id删除收货地址
	 *
	 * @param addressId
	 * @return
	 */
	@DeleteMapping(value = "/delete/{addressId}")
	@ApiOperation(value = "根据收货地址id删除收货地址")
	public Result deleteById(@PathVariable(name = "addressId", required = true)
							 @ApiParam(value = "addressId", required = true)
									 String addressId) {
		if (addressId == null) {
			return Result.error("删除失败");
		}
		boolean result = addressService.removeById(addressId);
		return result ?
				Result.success("删除成功")
				:
				Result.error("删除失败");
	}

	/**
	 * 修改收货地址
	 *
	 * @param address
	 * @return
	 */
	@ApiOperation(value = "根据收货地址id修改收货地址")
	@PutMapping
	public Result updateProduct(@RequestBody
								@ApiParam(value = "收货地址Address对象", required = true)
										Address address) {
		if (StringUtils.isEmpty(address.getId())) {
			return Result.error("修改失败");
		}
		boolean result = addressService.updateById(address);
		return result
				?
				Result.success("修改成功")
				:
				Result.error("修改失败");
	}

	/**
	 * 根据用户查看对应的收货地址
	 *
	 * @param userId
	 * @return
	 */
	@GetMapping("findByUserId/{userId}")
	@ApiOperation("根据用户查看对应的收货地址")
	public Result FindByUser(@PathVariable(value = "userId") @ApiParam("用户id") String userId) {
		if (StringUtils.isEmpty(userId)) {
			return Result.error("请输入userId");
		}
		return Result.success().data(addressService.findByUserId(userId));
	}

	/**
	 * 根据收货地址id查看对应地址
	 */
	@GetMapping("findById/{addressId}")
	@ApiOperation("根据收货地址id查看对应地址")
	public Result FindByAddressId(@PathVariable(value = "addressId") @ApiParam("收货地址id") String addressId) {
		if (StringUtils.isEmpty(addressId)) {
			return Result.error("请输入收货地址id");
		}
		return Result.success().data(addressService.getById(addressId));
	}

	/**
	 * 分页查看收货地址
	 *
	 * @return
	 */
	@GetMapping("/{size}/{current}")
	@ApiOperation("分页查看收货地址")
	public Result acceptProduct(@PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
								@PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current) {
		size = size <= 0 ? 5 : size;
		current = current <= 0 ? 1 : current;
		Page<Address> page = new Page(current, size);
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.orderByDesc("gmt_create");
		addressService.page(page, wrapper);
		return Result.success().data(page);
	}

	/**
	 * 按用户分页查看收货地址
	 *
	 * @return
	 */
	@GetMapping("/{userId}/{size}/{current}")
	@ApiOperation("按用户分页查看收货地址")
	public Result addressUserPage(@PathVariable(name = "userId") @ApiParam(required = true, value = "用户id") String userId,
								  @PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
								@PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current) {
		size = size <= 0 ? 5 : size;
		current = current <= 0 ? 1 : current;
		Page<Address> page = new Page(current, size);
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("user_id", userId);
		wrapper.orderByDesc("gmt_create");
		addressService.page(page, wrapper);
		return Result.success().data(page);
	}
}

